package l1j.server.server.clientpackets;

import l1j.server.echo.ClientThread;
import l1j.server.server.model.L1Character;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_Message_YN;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.utils.FaceToFace;

import java.util.logging.Level;
import java.util.logging.Logger;
// Referenced classes of package l1j.server.server.clientpackets:
// ClientBasePacket

/**
 * 處理收到由客戶端傳來決鬥的封包
 */
public class C_Fight extends ClientBasePacket {
	private static final Logger _log = Logger.getLogger(C_Fight.class.getName());
	private static final String C_FIGHT = "[C] C_Fight";

	@Override
	public String getType() {
		return C_FIGHT;
	}

	@Override
	public void start(final byte[] decrypt, final ClientThread client) {
		try {
			// 資料載入
			this.read(decrypt);

			final L1PcInstance pc = client.getActiveChar();
			if (pc == null || pc.isGhost() ||pc.isInHell()) {
				return;
			}
			final L1Character target = FaceToFace.faceToFace(pc);
			if (target != null) {
				if(target instanceof L1PcInstance){
					L1PcInstance cha = (L1PcInstance)target;
					if (!cha.isParalyzed()) {
						if (pc.getFightId() != 0) {
							pc.sendPackets(new S_ServerMessage(633)); // \f1你已經與其他人決鬥中。
							return;
						}
						else if (cha.getFightId() != 0) {
							cha.sendPackets(new S_ServerMessage(634)); // \f11對方已經與其他人決鬥中。
							return;
						}
						pc.setFightId(cha.getId());
						cha.setFightId(pc.getId());
						cha.sendPackets(new S_Message_YN(630, pc.getName())); // 要與你決鬥。你是否同意？(Y/N)
					}
				}
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);

		} finally {
			this.over();
		}
	}

}
